import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', name: 'login', component: Login },
  {
    path: '/home',
    name: 'home',
    redirect: '/welcome',
    component: () => import(/* webpackChunkName:"Home" */'../components/Home.vue'),
    children: [{
      path: '/welcome',
      name: 'welcome',
      component: () => import(/* webpackChunkName:Welcome */'../components/Welcome.vue')
    },
    {
      path: '/users',
      name: 'users',
      component: () => import(/* webpackChunkName:Welcome */'../components/user/Users.vue')
    },
    {
      path: '/roles',
      name: 'roles',
      meta: {
        keepAlive: true
      },
      // 路由钩子函数
      // beforeEnter (to, from, next) {
      //   // console.log(to.path, from.path)
      //   if (from.path === '/users') {
      //     to.meta.keepAlive = false
      //   } else {
      //     to.meta.keepAlive = true
      //   }
      //   next()
      // },
      component: () => import(/* webpackChunkName:Role */'../components/permission/Roles.vue')
    },
    {
      path: '/rights',
      name: 'rights',
      component: () => import(/* webpackChunkName:rights */'../components/permission/rights.vue')
    }
    ]
  },
  { path: '/echarts', name: 'echarts', component: () => import(/* webpackChunkName:"Echarts" */'../components/Echarts.vue') },
  { path: '/grade', name: 'grade', component: () => import(/* webpackChunkName:"grade" */'../components/selfTemplate/Grade.vue') },
  { path: '/temp', name: 'temp', component: () => import(/* webpackChunkName:"grade" */'../components/selfTemplate/Temp.vue') }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
